from .course_pagination import PageNumberPagination
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from . import models
from . import serializer
from rest_framework.filters import OrderingFilter, SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
from .filters import CourseFilterSet


class CourseCategoryView(GenericViewSet, ListModelMixin):
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseModelSerializer
    pagination_class = PageNumberPagination

    # 过滤和排序
    # filter_backends = [DjangoFilterBackend, OrderingFilter, SearchFilter]
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    ordering_fields = ['id', 'price', 'students']

    # search_fields = ['course_category', ]

    filter_fields = ['course_category']

    # 通过django-filter扩展过滤
    # filter_backends = [DjangoFilterBackend]

    # 原来是配置字段，现在配置类
    # filter_class = CourseFilterSet


"""
django-filters指定以某个字段过滤有两种方式
第一种：
    配置类：
        filter_backends = [OrderingFilter]
    配置字段：
        filter_fields = ['course_category']

第二种：
    配置类：
        filter_backends = [OrderingFilter]
        
    # 自己写的类
        class CourseFilterSet(FilterSet):
            class Meta:
                model = models.Course
                fields = ['course_category']
                
        filter_class = CourseFilterSet  

第三种：实现区间过滤
    class CourseFilterSet(FilterSet):
        min_price = filters.NumberFilter(
            field_name='price',
            lookup_expr='gte'
        )
        max_price = filters.NumberFilter(
            field_name='price',
            lookup_expr='lte'
        )
    
        class Meta:
            model = models.Course
            fields = ['course_category', 'min_price', 'max_price']
    
    # 配置类：
        filter_backends = [OrderingFilter]

    # 自己写的类 
        filter_class = CourseFilterSet  

"""

"""
排序：
按id正序倒序排序，按price正序倒序排列
使用：
    http://127.0.0.1:8000/course/free/?ordering=-id
配置类：
    filter_backends = [OrderingFilter]
配置字段：   
    ordering_fields = ['id', 'price']
    
    
内置过滤：
按照price过滤(表自有的字段直接过滤)
使用：
    http://127.0.0.1:8000/course/free/?search=39
配置类：
    filter_backends = [SearchFilter]
配置字段：
    search_fields = ['price']

    
扩展： django-filter
安装： pip install django-filter
支持自有字段的过滤，还支持外键字段的过滤

使用：
http://127.0.0.1:8000/course/free/?course_category=1
# 过滤分类为1（即python的课程）

配置类：
    filter_backends = [DjangoFilterBackend]
配置字段：
    filter_fields = ['course_category']

"""


# 课程章节接口
class CourseChapterView(GenericViewSet, ListModelMixin):
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True)
    serializer_class = serializer.CourseChapterSerializer

    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course', ]


# 搜索接口
class CourseSearchView(GenericViewSet, ListModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseModelSerializer
    pagination_class = PageNumberPagination

    # 过滤和排序
    filter_backends = [SearchFilter]
    search_fields = ['name', ]

